﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

#region Land
namespace backoffice.Data
{
    partial class Land
    {
        public void setLand(Land l)
        {
            this.LandID = l.LandID;
            this.UserID = l.UserID;
            this.ZoneID = l.ZoneID;
            this.Length = l.Length;
            this.Width = l.Width;
            this.Acre = l.Acre;
            this.CreationDate = l.CreationDate;
            this.Name = l.Name;
        }
    }
}
#endregion

#region dataLand
namespace backoffice.Data
{
    public class dataLand
    {
        #region Create
        public static void createLand(Land l)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                l.LandID = Guid.NewGuid();
                l.CreationDate = DateTime.Now;

                db.Lands.InsertOnSubmit(l);
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static void createPlant_Land(Plants_Land pl)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                pl.PlantLandID = Guid.NewGuid();
                pl.PlantationDate = DateTime.Now;

                db.Plants_Lands.InsertOnSubmit(pl);
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Get
        public static Land getLand(Guid guid)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Lands.FirstOrDefault(l => l.LandID == guid);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Land> getLands(Expression<Func<Land, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Lands.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Plants_Land> getLand_Plants(Expression<Func<Plants_Land, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Plants_Lands.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Set
        public static Boolean changeLand(Land l)
        {
            try
            {
                if (l == null) return false;
                else
                {
                    db2growDataContext db = new db2growDataContext();

                    Land res = db.Lands.FirstOrDefault(land => land.LandID == l.LandID);
                    if (res == null) return false;
                    else
                    {
                        res.setLand(l);
                        db.SubmitChanges();
                        return true;
                    }
                }

            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Delete
        public static Boolean deletePlant_Land(Plants_Land pl)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                if (db.Plants_Lands.Contains(pl))
                {
                    Plants_Land res = db.Plants_Lands.FirstOrDefault(p_l => p_l.PlantLandID == pl.PlantLandID);

                    if (res == null) return false;
                    else
                    {
                        db.Plants_Lands.DeleteOnSubmit(res);
                        db.SubmitChanges();
                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static Boolean deleteLand(Land l)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                if (db.Lands.Contains(l))
                {
                    Land res = db.Lands.FirstOrDefault(land => land.LandID == l.LandID);
                    
                    if (res == null) return false;
                    else
                    {
                        // Delete plants_land
                        IQueryable<Plants_Land> pl = db.Plants_Lands.Where((Plants_Land p_l) => p_l.LandID == res.LandID);
                        db.Plants_Lands.DeleteAllOnSubmit(pl);

                        // Delete reading
                        IQueryable<Reading> r = db.Readings.Where((Reading rd) => rd.LandID == res.LandID);
                        db.Readings.DeleteAllOnSubmit(r);

                        // Delete land
                        db.Lands.DeleteOnSubmit(res);
                        db.SubmitChanges();
                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion
    }
}
#endregion